package com.example.bailin.giftsay.hot.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;
import com.example.bailin.giftsay.entity.GuanZhuEntity;
import com.example.bailin.giftsay.hot.HotPLFragment;
import com.example.bailin.giftsay.hot.HotXQFragment;
import com.example.bailin.giftsay.hot.adapter.HotVpAdapter;
import com.example.bailin.giftsay.tool.DBTool;
import com.example.bailin.giftsay.tool.UrlProvide;

import java.util.ArrayList;

/**
 * Created by bailin on 16/8/29.
 */
public class HotNextActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager vp;
    private TabLayout tabLayout;
    private ImageView ivHotnextReturn;
    private RelativeLayout rl;
    private ImageView ivCollect;
    private String url;
    private String price;
    private int counts;
    private String name;
    private String userName;
    private int type = 2;

    @Override
    protected int setLayout() {
        return R.layout.activity_hotnext;
    }

    @Override
    protected void initView() {
        ivCollect = bindView(R.id.iv_collect);
        rl = bindView(R.id.rl_hotnext);
        vp = bindView(R.id.vp_hotnext);
        tabLayout = bindView(R.id.tablayout_hotnext_id);
        ivHotnextReturn = bindView(R.id.iv_hotnext_return);
        ivHotnextReturn.setOnClickListener(this);
        ivCollect.setOnClickListener(this);

    }

    @Override
    protected void initData() {
//        rl.bringToFront();
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        tabLayout.setTabTextColors(Color.WHITE, Color.WHITE);
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> strings = new ArrayList<>();
        fragments.add(new HotXQFragment());
        fragments.add(new HotPLFragment());
        strings.add("详情");
        strings.add("评论");
        HotVpAdapter adapter = new HotVpAdapter(getSupportFragmentManager());
        adapter.setStrings(strings);
        adapter.setFragments(fragments);
        vp.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp);

        SharedPreferences shared = getSharedPreferences(UrlProvide.NAME, MODE_PRIVATE);
        userName = shared.getString("username", "未登录");

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        price = intent.getStringExtra("price");
        counts = intent.getIntExtra("counts", 0);
        name = intent.getStringExtra("name");

        if (!userName.equals("未登录")) {
            DBTool.getInstance().queryGuanZhu("userName = ?", "title = ?", userName, name, new DBTool.QueryListener() {
                @Override
                public void onQueryComplete(ArrayList<GuanZhuEntity> arrayList) {
                    if (arrayList.size() != 0) {
                        ivCollect.setImageResource(R.mipmap.ic_action_compact_favourite_selected);
                        type = 1;
                    }
                }
            });
        } else {
            ivCollect.setImageResource(R.mipmap.ic_action_compact_favourite_normal);
        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_hotnext_return:
                finish();
                break;

            case R.id.iv_collect:
                if (!userName.equals("未登录")) {
                    if (type == 2) {
                        ivCollect.setImageResource(R.mipmap.ic_action_compact_favourite_selected);
                        ScaleAnimation sa = new ScaleAnimation(0, 1.5f, 0, 1.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        sa.setDuration(200);
                        sa.setRepeatCount(0);
                        sa.setFillAfter(false);
                        ivCollect.startAnimation(sa);

                        GuanZhuEntity entity = new GuanZhuEntity();
                        entity.setCount("" + counts);
                        entity.setData(System.currentTimeMillis());
                        entity.setImgUrl(url);
                        entity.setPrice(price);
                        entity.setTitle(name);
                        entity.setUserName(userName);
                        entity.setType("1");
                        DBTool.getInstance().insertEntity(entity);
                        type = 1;
                        Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();

                    } else {
                        ivCollect.setImageResource(R.mipmap.ic_action_compact_favourite_normal);
                        DBTool.getInstance().deleteGuanZhu("userName = ?", "title = ?", userName, name);
                        type = 2;
                        Toast.makeText(this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }
}